#include<iostream>
using namespace std;
struct treenode{
    treenode * left=NULL;
    treenode * right=NULL;
    int data;
};
treenode * createtree(){
    int x;
    cin>>x;
    if(x==0)return NULL;
    treenode * root=new treenode();
    root->data=x;
    root->left=createtree();
    root->right=createtree();
    return root;
}
void printzg(treenode * root){
    if(root->left!=NULL)printzg(root->left);
    cout<<root->data<<" ";
    if(root->right!=NULL)printzg(root->right);
}
void del(treenode *&root){
    if(root==NULL)return;
    del(root->left);
    del(root->right);
    delete(root);
    root=NULL;
}
bool delnum(treenode *&root ,int num){
    if(root==NULL)return false;
    if(root->data==num){
        del(root);return true;
    }
    if(delnum(root->left,num))return true;
    else return delnum(root->right,num);
}//空返回。找到直接删除即可。否则左右删除
int main(){
    treenode * root=createtree();
    int m;
    cin>>m;
    for(int i=1;i<=m;i++){
        int num;
        cin>>num;
        if(delnum(root,num)){
            printzg(root);
        }
        else cout<<0;
        cout<<endl;
        
    }
    return 0;
}